<template>
  <div>
    <CustomButton title="编辑" class="mg-bottom-16" @click="isShowDialog=true"></CustomButton>
    <div class="product-brief">
      <div class="flex">
        <div class="flex-sub flex">
          <div class="title">产品名称</div>
          <div class="detail flex-sub">{{ formData.name }}</div>
        </div>
        <div class="flex-sub flex">
          <div class="title">物模型</div>
          <div class="detail flex-sub">{{ formData.metaDataVo && formData.metaDataVo.name }}</div>
        </div>
        <div class="flex-sub flex">
          <div class="title">创建时间</div>
          <div class="detail flex-sub">{{ formData.createTime|dateTimeFormatter }}</div>
        </div>
      </div>
      <div class="flex">
        <div class="flex-sub flex">
          <div class="title">接入设备数</div>
          <div class="detail flex-sub justify-between">
            {{ formData.deviceCount || '0' }}
            <el-button
              class="mg-left-34"
              style="padding: 0;color: #0084EE;"
              type="text"
              @click="handleNavToDeployDevice"
            >
              前往管理
            </el-button>
          </div>
        </div>
        <div class="flex-sub flex">
          <div class="title">入网方式</div>
          <div class="detail flex-sub">{{ formData.netTypeName }}</div>
        </div>
        <div class="flex-sub flex">
          <div class="title">发布状态</div>
          <div class="detail flex-sub flex">
            <span class="pd-right-4">{{ formData.publishStateName }}</span>
            <!--<PublishStateSwitch v-model="formData.publishState" />-->
          </div>
        </div>
      </div>
      <div class="flex">
        <div class="flex flex-sub">
          <div class="title">产品图标</div>
          <div class="detail flex-sub">
            <img :src="formData.icon" alt="" />
          </div>
        </div>
        <div class="flex" style="flex: 2;">
          <div class="title">备注说明</div>
          <div class="detail flex-sub">{{ formData.remark }}</div>
        </div>
      </div>
    </div>
    <AddProduct
      v-if="isShowDialog"
      title="编辑产品"
      :key="productKey"
      :visible.sync="isShowDialog"
      :value="formData"
      @change="handleProductChange"
    />
  </div>
</template>

<script>
import { constUtils } from '../../mixin'
import { apiFindProductById } from '../api'
import { mapMutations } from 'vuex'
import dayjs from 'dayjs'
import CustomButton from '@/components/custom-button'
import AddProduct from './add-product'

export default {
  name: 'ProductBrief',
  components: {
    // PublishStateSwitch,
    CustomButton,
    AddProduct,
  },
  mixins: [constUtils],
  props: {
    value: { type: Object, required: true },
  },
  data() {
    return {
      isShowDialog: false,
      formData: {},
      items: [],
      productKey: 0,
    }
  },
  computed: {},
  watch: {
    value() {
      this.formData = this.value
    },
    formData() {
      const subTitle = `
            <span>
              <span>${this.formData.name}</span>
              <span class="mg-left-40">最近修改时间：${dayjs(this.formData.createTime).format(
                'YYYY-MM-DD hh:mm:ss'
              )}</span>
            </span>
          `
      this.setRouterSubTitle(subTitle)
    },
  },
  mounted() {},
  methods: {
    ...mapMutations({
      setRouterSubTitle: 'user/SET_ROUTER_SUBTITLE',
    }),
    handleNavToDeployDevice() {
      this.$router.push({ path: '/device/deploy-device' })
    },
    handleProductChange() {
      this.findProductById(this.formData.id)
    },
    async findProductById(productId) {
      this.formData = await apiFindProductById(productId)
    },
  },
  beforeDestroy() {
    this.setRouterSubTitle(null)
  },
}
</script>

<style scoped lang="scss">
.product-brief {
  border: 1px solid #999;
  border-bottom: none;
  border-right: none;
  .flex {
    .title {
      font-weight: bold;
      color: #333;
      text-align: center;
      background-color: #f2f2f2;
    }

    .detail {
      display: flex;
      color: #999;
      align-items: center;

      img {
        width: vw(20px);
        height: auto;
      }
    }

    .title,
    .detail {
      width: 200px;
      padding: 0 20px;
      line-height: vw(40px);
      border-right: 1px solid #999;
      font-size: 14px;
    }
  }

  .flex {
    .title,
    .detail {
      border-bottom: 1px solid #999;
    }
  }
}
</style>
